home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Dreamweaver 3 / Configuration / Commands / Set Color Scheme.js < prev    next >
Encoding:
Text File  |  1999-12-01  |  7.1 KB  |  199 lines

  1. //  Copyright 1998 Macromedia, Inc. All rights reserved.
  2.  
  3. //form fields:
  4. //Background - multiple list listing background colors
  5. //Text - multiple list listing text colors. Contents change when new Background item picked.
  6.  
  7. //******************* GLOBALS **********************
  8.  
  9. var helpDoc = MM.HELP_cmdSetColorScheme;
  10.  
  11. //******************* API **********************
  12.  
  13. function commandButtons()
  14. {
  15.     return new Array(BTN_OK,    "applyColorScheme();window.close()",
  16.                    BTN_Apply, "applyColorScheme()",
  17.                    BTN_Cancel,"window.close()",
  18.                    BTN_Help,  "displayHelp()")
  19. }
  20.  
  21. function canAcceptCommand()
  22. {
  23.   return ( (dw.getFocus() == 'document') 
  24.     && (dreamweaver.getDocumentDOM())
  25.     && (dreamweaver.getDocumentDOM("document").body) );
  26. }
  27.  
  28.  
  29. //******************* LOCAL FUNCTIONS **********************
  30. //function: initializeUI
  31. //arguments: none
  32. //return value: none
  33. //description: called from body onload handler, fills the background list widget
  34. //with colorSchemes[i], selects the first item, then fills the Text list with
  35. //properties of colorSchemes[i]
  36. function initializeUI()
  37. {
  38.     var Form = document.forms[0];
  39.     var backgroundList = Form.Background; 
  40.     var textList = Form.Text;
  41.     //colorSchemes(),defined in colorSchemes.js, returns a multi-dimensional
  42.     //array of of the form:
  43.     // colorSchemes[backgroundColor][backgroundColorIndex][property]
  44.     var Schemes = colorSchemes(); 
  45.                                            
  46.     //populate Background list...
  47.     var counter=0;
  48.     for (i in Schemes){
  49.     //properties added to the Array object as prototypes in other Dreamweaver files
  50.     //appear as properties when executing for i in [object] loops. Following conditional check
  51.     //ensures only array members of Schemes appear in Background list widget menu.
  52.       if (Schemes[i][0]) 
  53.       {
  54.         backgroundList.options[counter] = new Option(BG_COLOR[i]);
  55.         backgroundList.options[counter++].value = i;
  56.       }
  57.     }
  58.  
  59.     backgroundList.selectedIndex=0; //select first item of Background list
  60.  
  61.     //populate Text list based on selected list item in Background list
  62.     populateTextColorsList(backgroundList.options[backgroundList.selectedIndex].value);
  63. }
  64.  
  65.  
  66. //function: previewColorScheme
  67. //arguments:backgroundColorIndex
  68. //return value:none
  69. //description: called when an item in the text list widget is clicked,
  70. //previews the chosen color scheme on a table 1 cell high and 1 cell wide
  71. function previewColorScheme(backgroundColorInd)
  72. {
  73.     var selInd = document.forms[0].Background.selectedIndex;
  74.     var backgroundColor = document.forms[0].Background.options[selInd].text;
  75.     var tableCellNode = findObject("text").parentNode.parentNode.parentNode;
  76.  
  77.     //format background color and text 
  78.     with (colorSchemes()[backgroundColor][backgroundColorInd])
  79.     {
  80.         //change background color of table cell
  81.         tableCellNode.setAttribute("bgcolor",bgcolor);
  82.         //change colors of font nodes & update the text to reflect new color value
  83.         changeFontNodePreviewColor('text',text); 
  84.         changeFontNodePreviewColor('link',link);  
  85.         changeFontNodePreviewColor('alink',alink);  
  86.         changeFontNodePreviewColor('vlink',vlink);  
  87.     }
  88. }
  89.  
  90. //function:changeFontNodePreviewColor
  91. //arguments: docPropName, e.g.: text,link,vlink
  92. //           docPropValue,e.g.:'blue','#FFFF00'
  93. //return value: none
  94. //description: 1.updates the text representing the font property ('Text Color')
  95. //to the correct color. 2.changes the text to reflect the new color value,
  96. //e.g.: Text Color: #FFFF00
  97. function changeFontNodePreviewColor(docPropName,docPropValue)
  98. {
  99.     var fontNode=findObject(docPropName).childNodes.item(0).childNodes.item(0)
  100.     var fontNodeText = fontNode.innerHTML;
  101.     var colonAt = fontNodeText.indexOf(":");
  102.     var niceName = fontNodeText.substring(0,colonAt+1) + docPropValue;
  103.  
  104.     fontNode.setAttribute('color',docPropValue);
  105.     fontNode.innerHTML = niceName;                                   
  106. }
  107.  
  108. //function:applyColorScheme
  109. //arguments:none
  110. //return value:none
  111. //purpose:applies the chosen color scheme to the user's document
  112. function applyColorScheme()
  113. {
  114.     var currSelection = dreamweaver.getSelection();  //get current selection
  115.     var textList = document.forms[0].Text;
  116.     var backgroundList = document.forms[0].Background;
  117.     var bodyTag = dreamweaver.getDocumentDOM("document").body;
  118.     var selInd=backgroundList.selectedIndex;
  119.     var backgroundColor = backgroundList.options[selInd].text;
  120.     var textColors;
  121.     selInd = textList.selectedIndex;
  122.     textColors = textList.options[selInd].value
  123.  
  124.     with (colorSchemes()[backgroundColor][textColors])
  125.     {
  126.         bodyTag.setAttribute("bgcolor",bgcolor);
  127.         bodyTag.setAttribute("text",text);
  128.         bodyTag.setAttribute("link",link);
  129.         bodyTag.setAttribute("alink",alink);
  130.         bodyTag.setAttribute("vlink",vlink);
  131.     }
  132.     //reset current selection
  133.     dreamweaver.setSelection(currSelection[0],currSelection[1]);  
  134. }
  135.  
  136. //function: populateTextColorsList
  137. //aguments: backgroundColor, e.g.: red
  138. //return value: none
  139. //description: called when a new item in the background list widget is picked,
  140. //updates the Text and Links list to reflect text and link color choices
  141. //available with the new background color
  142. function populateTextColorsList(backgroundColor)
  143. {
  144.     var counter=0;
  145.     var Schemes=colorSchemes();
  146.     var textList = document.forms[0].Text;
  147.     var currListLen = textList.options.length; //get # of items in current list
  148.  
  149.     //clear current list
  150.     //for (i=0;i<currListLen;i++)
  151.     //textList.options[0] = null;
  152.  
  153.     for (i in Schemes[backgroundColor])
  154.     {     //populate text list
  155.         //properties added to the Array object as prototypes in other Dreamweaver
  156.         //files apppear as properties when executing for i in [object] loops.
  157.         // Following conditional check ensures only array members of Schemes
  158.         // appear in Text list widget menu.
  159.         if (i==parseInt(i))
  160.         { //check that i is property of colorSchemes
  161.             textList.options[counter] = new Option(Schemes[backgroundColor][i]['name']);
  162.             textList.options[counter++].value = i;
  163.         }
  164.     }
  165.     //kill any leftover previous names
  166.     for (i=currListLen-1;i>counter-1;i--){
  167.         textList.options[i] = null
  168.     }
  169.  
  170.     textList.selectedIndex=0; //select first item of Text list
  171.  
  172.     //format preview area according to selected item in Text list
  173.     previewColorScheme(textList.options[textList.selectedIndex].value);
  174. }
  175.  
  176. //******************* GENERIC FUNCTIONS **********************
  177.  
  178. function findObject(objName,  parentObj) {
  179.   var i,tempObj="",found=false,curObj = "";
  180.   var NS = (navigator.appName.indexOf("Netscape") != -1);
  181.   if (!NS && document.all) curObj = document.all[objName]; //IE4
  182.   else {
  183.     parentObj = (parentObj != null)? parentObj.document : document;
  184.     if (parentObj[objName] != null) curObj = parentObj[objName]; //at top level
  185.     else { //if in form
  186.       if (parentObj.forms) for (i=0; i<parentObj.forms.length; i++) {  //search level for form object
  187.         if (parentObj.forms[i][objName]) {
  188.           curObj = parentObj.forms[i][objName];
  189.           found = true; break;
  190.       } }
  191.       if (!found && NS && parentObj.layers && parentObj.layers.length > 0) {
  192.         parentObj = parentObj.layers;
  193.         for (i=0; i<parentObj.length; i++) { //else search for child layers
  194.           tempObj = findObject(objName,parentObj[i]); //recurse
  195.           if (tempObj) { curObj = tempObj; break;} //if found, done
  196.   } } } }
  197.   return curObj;
  198. }
  199.